/** 画图控制器，面向工具：切换工具、分发事件
 * 一.公共属性：
 *   _tools - 画图工具
 *  _toolType - 当前激活工具名称
 *  _clipbrdType - 剪贴板类型 in内部 out外部
 *  事件监听、分发（略）
 * 二、外部发送事件
 *  _chooseToolCallBack {Function} - 切换工具时回调
 *  _setOptionCallBack {Function} - 切换配置时回调
 * */
import {defaultColors} from './js/colorHelper';
import {Pencil} from './js/tools/pencil';
import {Eraser} from './js/tools/eraser';
import {Fill} from './js/tools/fill';
import {Area} from './js/tools/area';
import {ColorPicker} from './js/tools/color_picker';
import {TextArea} from './js/tools/text_area';
import {EventManager} from "./js/event_manager";

export class Controller extends EventManager {
  constructor(option = {}, chooseToolCallBack, setOptionCallBack) {
    super(option);
    this.defaultColors = defaultColors;
    this._toolType = '';
    this._tools = {};
    this.activeTool = '';
    this._chooseToolCallBack = chooseToolCallBack || function() {};
    this._setOptionCallBack = setOptionCallBack || function() {};
    // 初始化工具
    this.initTools();
  }

  /** 工具 */
  // 初始化工具
  initTools() {
    this._tools = {
      Pencil: new Pencil(),
      Eraser: new Eraser(),
      Fill: new Fill(),
      Area: new Area(),
      ColorPicker: new ColorPicker(),
      TextArea: new TextArea(),
    };
    let t = this._tools;
    for (let i in t) {
      t[i].id = i;
      t[i].ctx = this.ctx;
      t[i].canvas = this.canvas;
      t[i].option = this.option;
      t[i].global = this;
    }
    this.chooseTool('Pencil');
  }

  /** 外部交互，发送事件 */
  // 状态管理并通知外部
  setOption() {
    super.setOption(...arguments);
    this._setOptionCallBack && this._setOptionCallBack(arguments[0]);
  }
  // 切换工具
  chooseTool(type) {
    const oldTool = this._tools[this._toolType];
    const tool = this._tools[type];
    // 下发事件
    if (!!this._toolType && this._toolType !== type) {
      typeof oldTool.stopTool == 'function' && oldTool.stopTool();
    }
    typeof tool.readyTool == 'function' && tool.readyTool();
    // 切换工具
    this._toolType = type;
    this._chooseToolCallBack(type);
    this.activeTool = tool;
    return tool;
  }

  /** 事件监听、派发 */
  // 过程事件
  onBegin(point, event) {
    try {
      this._tools[this._toolType].onBegin(point, event);
    } catch (e) {
    }
  }

  onMove(from, to) {
    try {
      this._tools[this._toolType].onMove(from, to);
    } catch (e) {
    }
  }

  onEnd(point) {
    try {
      this._tools[this._toolType].onEnd(point);
    } catch (e) {
    }
  }
  // 剪贴板操作
  set vClipbrd ({base64, width, height}) {
    const img = new Image(width, height);
    img.width = width;
    img.height = height;
    img.src = base64;
    img.onload = _ => {
      this._vClipbrd = img;
      this.setOption({
        clipbrdType: 'in'
      });
    };
  }
  get vClipbrd () {
    return this._vClipbrd;
  }
  onClipbrdEvent(type, img='') {
    if (type === 'paste') {
      this.chooseTool('Area');
      this._tools['Area'].onClipbrdEvent(type, img);
    } else if('Area' === this._toolType){
      this._tools['Area'].onClipbrdEvent(type, img);
    }
  }
}